package com.itxiaoxiong.chat.controller;

import com.itxiaoxiong.chat.mapper.FriendMapper;
import com.itxiaoxiong.chat.mapper.UserMapper;
import com.itxiaoxiong.chat.pojo.*;
import com.itxiaoxiong.chat.service.FriendService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class FriendController {

    @Autowired
    private FriendService friendService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FriendMapper friendMapper;

    /*
    根据名字查询用户
     */
    @GetMapping("/chatUsername")
    public Result selectFriend(@RequestParam String username){
        log.info("根据username查询用户:{}", username);
        User user = friendService.listName(username);
        return Result.success(user);
    }

    /*
    加好友
     */
    @PostMapping("/addFriend")
    public Result addFriend(@RequestBody FriendRelationship friendAdd) {
        log.info("添加好友: {}", friendAdd);
        return friendService.addFriend(friendAdd);
    }

    /*
    删好友
     */

    @DeleteMapping("/deleteFriend")
    public Result deleteFriend(@RequestBody FriendRelationship friendDelete) {
        log.info("删除好友: {}", friendDelete);
        Result result = friendService.deleteFriend(friendDelete);
        return result;
    }

    /*
    @GetMapping("/getFriends")
    public Result getFriends(@RequestParam Integer userId) {
        List<FriendRelationship> friends = friendService.getFriends(userId);

        return Result.success(friends);
    }
    */
    /*
    查询好友列表
     */
    @GetMapping("/getFriends")
    public Result getFriends(@RequestParam Integer userId) {
        log.error("friends = {}",userId);
        List<FriendRelationship> friends = friendService.getFriends(userId);
        log.error(friends.toString());
        //SelectFriend selectFriend = new SelectFriend();
        //selectFriend.setUserId(userId);
        List<User> friendsList = new ArrayList<>();
        for (FriendRelationship friend : friends) {
            if (friend.getUserId1()!= userId) {
               // friendIds.add(friend.getUserId1());
                User user = userMapper.listUserName(friend.getUserId1());
                friendsList.add(user);
            } else {
                //friendIds.add(friend.getUserId2());
                User user = userMapper.listUserName(friend.getUserId2());
                friendsList.add(user);
            }
        }
       // selectFriend.setFriendId(friendIds.toArray(new Integer[0]));
        Map<String,Object> result = new HashMap<>();
        result.put("friends",friendsList);
        return Result.success(result);
    }
    /*
    查询聊天记录
     */
    @GetMapping("/chatSelect")
    public Result chatMessage(@RequestParam Integer userId1, @RequestParam Integer userId2){
        boolean exists = friendMapper.checkIfFriendExists(userId1, userId2);
        if(exists) {
            List<ChatMessage> messages = friendService.getMessage(userId1, userId2);
            log.error(messages.toString());
            Map<String, Object> result = new HashMap<>();
            result.put("message", messages);
            User user1 = userMapper.listUserName(userId1);
            User user2 = userMapper.listUserName(userId2);
            List<User> userList = new ArrayList<>();
            userList.add(user1);
            userList.add(user2);
            result.put("member", userList);
            return Result.success(result);
        }else {
            return Result.error("不是好友了");
        }
    }

        /*
    发消息
     */
    @PostMapping("/chatAdd")
    public Result addMessage(@RequestBody ChatMessage chatMessage){
        log.info("发消息 : {}", chatMessage);
/*        ChatMessage cm = friendService.addMessage(chatMessage);*/
        friendService.addMessage(chatMessage);
        return Result.success();
    }

    /*
    发图片
     */
//    @PostMapping("/chatImageAdd")
//    public Result addImageMessage(@RequestBody ChatMessage chatMessage){
//        log.info("发图片:{}", chatMessage);
//        friendService.addImageMessage(chatMessage);
//        return Result.success();
//    }

    /*
    删除聊天记录
     */
    @DeleteMapping("/chatDelete")
    public Result deleteMessage(@RequestBody ChatMessage chatMessage){
        log.info("删除聊天记录");
        friendService.deleteMessage(chatMessage);
        return Result.success();
    }

    /*
    发送图片
     */
    @PostMapping("/addFile")
    public Result addFile(@RequestBody ChatMessage chatMessage){
        log.info("发送图片: {}", chatMessage);
        friendService.addFile(chatMessage);
        return Result.success();
    }
}
